package org.jarx.android.reader;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class ActivityHelper {

    public static final String EXTRA_SUB_ID = "subId";
    public static final String EXTRA_TAG_UID = "tagUid";
    public static final String EXTRA_ITEM_ID = "itemId";
    public static final String EXTRA_UNREAD_ONLY = "unreadOnly";
    public static final String EXTRA_QUERY = "query";
    public static final String EXTRA_CURSOR_POSITION = "cursorPosition";

    public static final int REQUEST_PREFS = 1;
    public static final int REQUEST_ITEM_ID = 2;

    static void bindTitle(Activity activity) {
        Context context = activity.getApplicationContext();
        String loginId = Prefs.getGoogleId(context);
        if (loginId != null) {
            StringBuilder buff = new StringBuilder(64);
            buff.append(activity.getText(R.string.app_name));
            buff.append(" - ");
            buff.append(loginId);
            activity.setTitle(buff);
        }
    }

    static void showToast(Context c, IOException e) {
        e.printStackTrace();
        showToast(c, c.getText(R.string.err_io)
            + " (" + e.getLocalizedMessage() + ")");
    }

    static void showToast(Context c, ReaderException e) {
        e.printStackTrace();
        showToast(c, e.getLocalizedMessage());
    }

    static void showToast(final Context c, final CharSequence text) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            public void run() {
                Toast.makeText(c, text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    static void startItemActivityBySubId(Context c, long subId) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_SUB_ID, subId);
        startItemActivity(c, intent);
    }

    static void startItemActivityByTagUid(Context c, String tagUid) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_TAG_UID, tagUid);
        startItemActivity(c, intent);
    }

    static void startItemActivity(Context c, Intent intent) {
        if (Prefs.isOmitItemList(c)) {
            intent.setClass(c, ItemActivity.class);
        } else {
            intent.setClass(c, ItemListActivity.class);
        }
        c.startActivity(intent);
    }

    static void startItemActivity(Context c) {
        startItemActivity(c, new Intent());
    }

    static CharSequence toLabelWithUnread(Subscription sub) {
        return toLabelWithUnread(sub.getTitle(), sub.getUnreadCount());
    }

    static CharSequence toLabelWithUnread(Tag tag, Activity a) {
        if (tag.getType() == Tag.TYPE_TAG_STARRED) {
            return toLabelWithUnread(a.getText(R.string.txt_star),
                tag.getUnreadCount());
        }
        return toLabelWithUnread(tag.getLabel(), tag.getUnreadCount());
    }

    static CharSequence toLabelWithUnread(CharSequence t, int unreadCount) {
        if (unreadCount == 0) {
            return t;
        }
        StringBuilder buff = new StringBuilder(t.length() + 8);
        buff.append(t);
        buff.append(" (");
        if (unreadCount >= 1000) {
            buff.append("1000+");
        } else {
            buff.append(unreadCount);
        }
        buff.append(")");
        return buff;
    }

    static Query getItemQuery(Subscription sub, Tag tag,
            boolean unreadOnly) {
        if (sub != null) {
            return ReaderManager.getItemQueryBySubId(sub.getId(),
                unreadOnly);
        }
        if (tag != null) {
            if (tag.getType() == Tag.TYPE_FOLDER) {
                return ReaderManager.getItemQueryBySubTagUid(tag.getUid(),
                    unreadOnly);
            }
            return ReaderManager.getItemQueryByTagUid(tag.getUid(),
                unreadOnly);
        }
        return ReaderManager.getItemQueryAll(unreadOnly);
    }

    static Item.FilterCursor moveToItemId(Item.FilterCursor csr,
            long itemId) {
        if (csr.isClosed()) {
            return csr;
        }
        if (itemId > 0) {
            int pos = csr.getPosition();
            if (pos != -1 && csr.getId() == itemId) {
                return csr;
            }
            boolean found = false;
            while (csr.moveToNext()) {
                if (csr.getId() == itemId) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                csr.moveToFirst();
            }
        } else {
            csr.moveToNext();
        }
        return csr;
    }
}
